<template>
  <div>
    <el-dialog
      :title="{ add: '新增用户', edit: '编辑用户' }[mode]"
      width="602px"
      :visible="isShow"
      @close="closeEvent"
    >
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model.trim="form.username" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="角色" prop="role_id">
          <el-select v-model="form.role_id" placeholder="请选择">
            <el-option label="超级管理员" value="1"></el-option>
            <el-option label="管理员" value="2"></el-option>
            <el-option label="老师" value="3"></el-option>
            <el-option label="学生" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option label="启用" :value="1"></el-option>
            <el-option label="禁用" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户备注" prop="remark">
          <el-input v-model="form.remark" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center">
          <el-button @click="closeEvent(value)">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { userAdd, userEdit } from '@/api/user.js'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  model: {
    prop: 'isShow'
  },
  data () {
    return {
      nameData: [],
      mode: 'add', // add,edit
      form: {
        username: '',
        email: '',
        phone: '',
        role_id: '',
        status: '',
        remark: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'change' },
          {
            validator: (rules, value, callback) => {
              if (value !== '') {
                const reg = this.nameData.map((value) => {
                  value = this.value
                })
                if (reg.includes(value)) {
                  callback(new Error('用户名已存在'))
                } else {
                  callback()
                }
              }
            }
          }
        ],
        email: [
          { required: true, message: '请正确输入邮箱地址', trigger: 'change' },
          {
            validator: (rules, value, callback) => {
              const reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入符合规则的邮箱'))
              }
            }
          }
        ],
        phone: [
          { required: true, message: '请正确输入电话号码', trigger: 'change' },
          {
            validator: (rules, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入符合规则的手机号'))
              }
            }
          }
        ],
        role_id: [{ required: true, message: '请选择角色', trigger: 'change' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      }
    }
  },
  // 用监听器监听isShow
  // 发生改变时只有更改成功之后 newVal为false时执行后面的代码 清除表单和验证
  watch: {
    isShow: {
      handler (newVal) {
        console.log('当前新的值', newVal)
        if (!newVal) {
          this.$emit('input', false)
          this.form = {
            username: '',
            email: '',
            phone: '',
            role_id: '',
            status: '',
            remark: ''
          }
          this.$nextTick(() => {
            this.$refs.form.resetFields()
          })
        }
      }
    }
  },
  methods: {
    // 关闭事件
    closeEvent () {
      this.$emit('input', false)
    },
    // 确认点击事件
    submit () {
      console.log('前面的', this.form)
      this.$refs.form.validate(async (result) => {
        if (result) {
          if (this.mode === 'add') {
            // 添加
            await userAdd(this.form)
            // 弹出提示信息 关闭弹窗 重新调用页面
            this.$message.success('添加成功')
            this.closeEvent()
            this.$emit('getData')
          } else if (this.mode === 'edit') {
            // 编辑
            const res = await userEdit(this.form)
            console.log('编辑后', res)
            this.$message.success('编辑成功')
            this.closeEvent()
            this.$emit('getData')
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  .el-dialog__header {
    padding: 20px 20px 10px;
    text-align: center;
    background: -webkit-linear-gradient(left, #03bffa, #119afa);
    .el-dialog__title {
      color: #fff;
    }
  }
  .el-icon-close:before {
    content: '\e6db';
    color: #fff;
  }
}
</style>
